from time import sleep

from PyQt5.QtCore import QThread, pyqtSignal
import requests
import json


class LoginThread(QThread):
    loginSignal = pyqtSignal(int)

    def __init__(self):
        super(LoginThread, self).__init__()
        self.username = ""
        self.pwd = ""

    def setUser(self, user, pwd):
        self.username = user
        self.pwd = pwd

    def run(self) -> None:
        sleep(1)
        host = 'http://127.0.0.1:8080/login'
        # payload = {"name": self.username, "pwd": self.pwd}
        # s = json.dumps(payload)
        s = "name=%s&pwd=%s" % (self.username, self.pwd)
        print(s)
        header = {
            'Accept-Encoding': 'gzip',
            'Authorization': '12347217-2401-40e4-9983-41bde0123456',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
        #     'Content-Type': 'application/json; charset=UTF-8'
        r = requests.post(host, data=s, headers=header)
        print(r.text)
        self.loginSignal.emit(int(r.text))
